package com.base.controller;

import com.base.po.coupon.QueryOrderResponse;
import com.base.po.coupon_prestore.CouponOrderPrestore;
import com.base.po.coupon_sdk.CouponInfoUtil;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.po.voip_coupon.CouponPayOrder;
import com.base.service.COUPON.*;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 365优惠券订单管理服务端
 *
 * @author K
 */
@Controller
@RequestMapping("/admin")
public class Index_CouponOrdersPrestore {
    @Autowired
    private UsersService usersService;
    @Autowired
    private SubscribersService subscribersService;
    @Autowired
    private CouponBrandPrestoreService couponBrandPrestoreService; // 分类信息
    @Autowired
    private CouponGoodsPrestoreService couponGoodsPrestoreService;
    @Autowired
    private CouponOrderPrestoreService couponOrderPrestoreService;
    @Autowired
    private CouponPayOrderService couponPayOrderService;

    /**
     * 订单首页
     *
     * @param request       参数
     * @param orderStatus   365订单状态
     * @param status        订单状态
     * @param currentPageNo 当前页
     * @return ModelAndView
     * @throws Exception
     */
    @RequestMapping("/couponOrderPrestoreIndex")
    public ModelAndView couponOrderPrestoreIndex(HttpServletRequest request, Integer orderStatus, Integer status, Integer currentPageNo) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        page.setCurrentPageNo(currentPageNo); // 设置当前页
        page.setRecordTotal(couponOrderPrestoreService.countAll(userid, null, orderStatus, status)); // 设置总条数
        List<CouponOrderPrestore> orders = couponOrderPrestoreService.selectAll(page, userid, null, orderStatus, status); // 订单集合
        for (CouponOrderPrestore order : orders){
            QueryOrderResponse query = CouponInfoUtil.queryOrder(order.getOrderno());
            if (query != null && query.getCode() == 200){
                if (order.getOrderstatus() != query.getData()){
                    order.setOrderstatus(query.getData());
                    couponOrderPrestoreService.updateByPrimaryKeySelective(order);
                }
            }

            Users users = usersService.selectByPrimaryKey(order.getParentid());
            order.setParentid(users.getUsername());
            Subscribers sub = subscribersService.selectByPrimaryKey(order.getUserid());
            order.setUserid(sub.getMobile());
            CouponPayOrder payOrder = couponPayOrderService.selectByPrimaryKey(order.getId());
            order.setProductid(payOrder.getProductname());
        }
        map.put("orders", orders); // 对象
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/index_CouponOrderPrestore", map);
    }
}
